package com.fr.report.script.function;

import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;
import com.fr.util.Utils;

public class REPLACE extends NormalFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    if ((paramArrayOfObject.length < 3) || (paramArrayOfObject.length > 4))
      return Primitive.ERROR_NAME;
    if (paramArrayOfObject.length == 3)
      return Utils.replaceAllString(Utils.objectToString(paramArrayOfObject[0]), Utils.objectToString(paramArrayOfObject[1]), Utils.objectToString(paramArrayOfObject[2]));
    if (paramArrayOfObject.length == 4)
    {
      Object localObject1 = paramArrayOfObject[0];
      int i = -1;
      Object localObject2 = paramArrayOfObject[1];
      if (localObject2 instanceof Number)
        i = ((Number)localObject2).intValue();
      int j = -1;
      Object localObject3 = paramArrayOfObject[2];
      if (localObject3 instanceof Number)
        j = ((Number)localObject3).intValue();
      if ((i < 1) || (j < 0))
        return Primitive.ERROR_VALUE;
      Object localObject4 = paramArrayOfObject[3];
      if (localObject1 != null)
      {
        String str = "" + localObject1;
        if (i >= str.length())
          return "";
        return str.substring(0, i - 1) + localObject4 + str.substring(i + j - 1);
      }
      return Primitive.ERROR_VALUE;
    }
    return Primitive.ERROR_VALUE;
  }

  public Function.Type getType()
  {
    return Function.TEXT;
  }

  public String getCN()
  {
    return "REPLACE(text, texttoreplace, replacetext):根据指定字符串，用其他文本来代替原始文本中的内容。\ntext：需要被替换部分字符的文本或单元格引用。\nregex：指定的字符串或正则表达式。\nreplacetext:需要替换部分旧文本的文本。\n示例：\nREPLACE(\"abcd\", \"a\", \"re\")等于\"rebcd\"。\nREPLACE(\"a**d\", \"**d\", \"rose\")等于\"arose\"。\nREPLACE(old_text,start_num,num_chars,new_text): 根据指定的字符数，用其他文本串来替换某个文本串中的部分内容。\nOld_text:需要被替换部分字符的文本或单元格引用。\nStart_num:需要用new_text来替换old_text中字符的起始位置。\nNum_chars:需要用new_text来替换old_text中字符的个数。\nNew_text:需要替换部分旧文本的文本。\n示例:\nREPLACE(\"0123456789\",5,4,\"*\")等于“0123*89”。\nREPLACE(\"1980\",3,2,\"99\")等于“1999”。";
  }

  public String getEN()
  {
    return "REPLACE(text, texttoreplace, replacetext): According to the specified string, some characters of the text will be replaced.\ntext:you want to replace some characters in the text\nregex:Specified string or regular expression.\nreplacetext:the text that will replace some characters in text.\nExample：\nREPLACE(\"abcd\", \"a\", \"re\")=\"rebcd\"。\nREPLACE(\"a**d\", \"**d\", \"rose\")=\"arose\"。\n\nREPLACE(old_text,start_num,num_chars,new_text): REPLACE replaces part of a text string, based on the number of characters you specify, with a different text string.\nOld_text is text in which you want to replace some characters.\nStart_num is the position of the character in old_text that you want to replace with new_text.\nNum_chars is the number of characters in old_text that you want REPLACE to replace with new_text.\nNew_text is the text that will replace characters in old_text.\n\nExample:\n   REPLACE(\"0123456789\",5,4,\"*\") = \"0123*89\"\n   REPLACE(\"1980\",3,2,\"99\") = \"1999\"";
  }
}